import Core from '@/core.js'
export default {
	data() {
		return {
			Core,
			hactive: 'active', // 控制买、卖货订单展示
			tactive: '', // 控制买、卖货订单展示
			hleft: 0, // 控制黄色下划线位置
			activeNum: 0, // 控制订单列表状态切换
			activeNum2: 0, // 控制订单列表状态切换
			// 0、全部 1、待付款 2、待确认 3、交易中 4、完成 5、定金退还中（等待对方同意退还）6、退款完成 （对方同意退还）7、已取消
			// 买车订单状态对应数据
			orderStatePage1: [{}, //占位
				{
					text: '待买家付款',
					state: 2,
					btn: ["立即付款", "取消交易"]
				}, //1、待付款
				{
					text: '待对方确认',
					state: 2,
					btn: ['订单详情', '提醒', "取消交易"]
				}, //2、待确认
				{
					text: '交易中',
					state: 2,
					btn: ['合同详情', '申请退款', '确认收货']
				}, //3、交易中
				{
					text: '交易完成',
					state: 2,
					btn: ['继续寻车']
				}, //4、完成 
				{
					text: '买家申请退款',
					state: 3,
					btn: ['退款进度']
				}, //5、买家申请退款
				{
					text: '定金退款成功',
					state: 2,
					btn: ['继续寻车']
				}, //6、退款完成 （对方同意退还
				{
					text: '订单已取消',
					state: 2,
					btn: ['查看订单']
				}, //7、已取消
				{
					text: '卖家取消交易',
					state: 3,
					btn: ['拒绝取消', "同意取消"]
				},
				{
					text: '卖家拒绝退款',
					state: 3,
					btn: ['确认收货']
				},
				{
					text: '买家拒绝取消',
					state: 3,
					btn: ['撤销拒绝']
				},
			],
			// 卖车订单状态对应数据
			orderStatePage2: [{}, //占位
				{
					text: '待买家付款',
					state: 2,
					btn: ["取消合同"]
				}, //1、待付款
				{
					text: '买方已支付定金',
					state: 2,
					btn: ["取消合同", '合同详情', '确认合同']
				}, //2、待确认
				{
					text: '交易中',
					state: 2,
					btn: ['取消交易']
				}, //3、交易中
				{
					text: '交易完成',
					state: 2,
					btn: ['发布车源']
				}, //4、完成 
				{
					text: '买家申请退款',
					state: 3,
					btn: ['拒绝退款', "确认退款"]
				}, //5、定金退还中
				{
					text: '定金退款成功',
					state: 2,
					btn: ['发布车源']
				}, //6、退款完成 （对方同意退还
				{
					text: '订单已取消',
					state: 2,
					btn: ['发布车源']
				}, //7、已取消
				{
					text: '卖家取消交易',
					state: 3,
					btn: ['退款进度']
				},
				{
					text: '卖家拒绝退款',
					state: 3,
					btn: ['撤销拒绝']
				},
				{
					text: '买家拒绝取消',
					state: 3,
					btn: []
				},
			],
			//分页
			Total: 10, //总条数 ,
			CurrentPage: 1, //当前页
			orderList: [],
			orderLists: [],
			orderNum: {
				total: 0,
				trading: 0,
				aftersale: 0,
				WaitConfirm: 0
			},
			bframe: false,
			bframe2: false,
			statuss: 0,
			showS: false,
			texts: '',
			jujueShow: false,
			contractid: '',
		}
	},
	onLoad() {
		this.getPageData(0)
		this.getTotal(Core.Bus.BuyerListTotal)
	},
	onReachBottom() {
		this.topRefresh()
	},
	methods: {
		topRefresh() {
			if (this.showS) {
				return
			}
			this.CurrentPage++
			console.log(this.CurrentPage)
			if (this.hactive) {
				this.getPageData(this.statuss)
			}
			if (this.tactive) {
				this.getSellCar(this.statuss)
			}
		},
		getTotal(url) {
			var req = {
				storeid: Core.CurrentUser().storeid,
			}
			Core.AjaxPost(url, req, (res) => {
				this.orderNum = res.data.Item
			})
		},
		// 买车订单
		getPageData(status) {
			var req = {
				Tid: Core.CurrentUser().uid,
				status: status,
				storeid: Core.CurrentUser().storeid,
				pageindex: this.CurrentPage,
				pagesize: 10
			}
			Core.AjaxPost(Core.Bus.ContractList, req, (res) => {
				if (res.data.Item.length === 0) {
					this.showS = true
					this.CurrentPage--
					console.log(this.CurrentPage)
					uni.showToast({
						title: "没有更多数据",
						icon: "none",
					})
					return
				}
				this.showS = false
				this.orderList = this.orderList.concat(this.filterColor(res.data.Item))
			})

		},
		// 卖车订单
		getSellCar(status) {
			var req = {
				Tid: Core.CurrentUser().uid,
				status: status,
				storeid: Core.CurrentUser().storeid,
				pageindex: this.CurrentPage,
				pagesize: 10
			}
			Core.AjaxPost(Core.Bus.SellContractList, req, (res) => {
				// this.orderList = res.data.Item
				if (res.data.Item.length === 0) {
					this.showS = true
					this.CurrentPage--
					uni.showToast({
						title: "没有更多数据",
						icon: "none",
					})
					return
				}
				this.showS = false
				this.orderLists = this.orderLists.concat(this.filterColor(res.data.Item))
			})
		},
		clickOrder(type) {
			if (type === 0) {
				this.hactive = 'active'
				this.tactive = ''
				this.hleft = 0
				this.activeNum2 = 0
				this.CurrentPage = 1
				this.orderLists = []
				this.getPageData(0)
				this.getTotal(Core.Bus.BuyerListTotal)
			}
			if (type === 1) {
				this.hactive = ''
				this.tactive = 'active'
				this.hleft = 351
				this.CurrentPage = 1
				this.activeNum = 0
				this.orderList = []
				this.getSellCar(0)
				this.getTotal(Core.Bus.SellListTotal)
			}
		},
		selectItem(flag, item) {
			if (this.statuss === item) {
				return
			}
			if (flag) {
				this.CurrentPage = 1
				this.statuss = item
				this.activeNum = item
				this.orderList = []
				this.getPageData(item)
			} else {
				this.CurrentPage = 1
				this.statuss = item
				this.orderLists = []
				this.activeNum2 = item
				this.getSellCar(item)
			}
		},
		// 重组颜色代码
		filterColor(data) {
			data.forEach(item => {
				let arr = JSON.parse(item.carcolour)
				item.carcolour = this.filtercolors(arr)
			})
			return data
		},
		filtercolors(data) {
			var arr = data.inimg.color.split('/')
			data.inimg.color = arr
			return data
		},
		// 点击买车订单按钮
		clickBtn(item, data) {
			this.contractid = data
			switch (item) {
				case "取消交易":
					this.bframe = true
					break;
				case "申请退款":
					this.requestFefund()
					break;
				case "确认收货":
					this.confirmReceipt()
					break;
				case "同意取消":
					this.agreeCancel()
					break;
				case "拒绝取消":
					this.jujueShow = true
					break;
				case "撤销拒绝":
					this.revokeRefuse()
					break;
				case "立即付款":
					uni.navigateTo({
						url: "/pages_Sxy/Sxy_payDeposit/Sxy_payDeposit?contractid=" + this.contractid
					})
					break;
				case "提醒":
					uni.showToast({
						title: "该功能还未开发",
						icon: "none",
					})
					break;
				case "继续寻车":
					uni.navigateTo({
						url: "/pages_Y/Y_Releasenewcar/Y_Releasenewcar"
					})
					break;
				default:
			}
		},
		// 卖车订单按钮
		clickBtn2(item, data) {
			this.contractid = data
			switch (item) {
				case "取消合同":
					this.bframe = true
					break;
				case "取消交易": //退款
					this.requestFefund()
					break;
				case "确认合同":
					this.confirmContract()
					break;
				case "确认退款":
					this.bframe2 = true
					break;
				case "拒绝退款":
					this.jujueShow = true
					break;
				case "撤销拒绝":
					this.revokeRefuse()
					break;
				case "确认发货":
					uni.showToast({
						title: "该功能还未开发",
						icon: "none",
					})
					break;
				default:
			}
		},
		// 取消交易（在对方还未确认时或者没有付款时）
		cancelTransaction() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid
			}
			Core.AjaxPost(Core.Bus.ContractCancel, req, (res) => {
				this.xiuGai()
			})
		},
		// 确认合同
		confirmContract() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid
			}
			Core.AjaxPost(Core.Bus.ContractConfirm, req, (res) => {
				this.xiuGai()
			})
		},
		// 确认收货
		confirmReceipt() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid
			}
			Core.AjaxPost(Core.Bus.ContractReceivingGoods, req, (res) => {
				this.xiuGai()
			})
		},
		// 申请退款
		requestFefund() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid
			}
			Core.AjaxPost(Core.Bus.ContractApply, req, (res) => {
				this.xiuGai()
			})
		},
		// 同意退款
		agreeCancel() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid,
				"type": 0,
				"remark": ""
			}
			Core.AjaxPost(Core.Bus.ContractRefund, req, (res) => {
				this.xiuGai()
			})
		},
		// 拒绝退款
		refuseCancel() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid,
				"type": 1,
				"remark": this.texts
			}
			Core.AjaxPost(Core.Bus.ContractRefund, req, (res) => {
				this.xiuGai()
			})
		},
		// 撤销拒绝
		revokeRefuse() {
			var req = {
				"contractid": this.contractid,
				"userid": Core.CurrentUser().uid,
				"type": 0,
				"remark": ""
			}
			Core.AjaxPost(Core.Bus.ContractRefund, req, (res) => {
				this.xiuGai()
			})
		},
		//跳转详情
		goDetail(item) {
			uni.navigateTo({
				url: '/pages_Sxy/Sxy_carOrderDetail/Sxy_carOrderDetail?contractid=' + item
			})
		},
		xiuGai() {
			if (this.hactive) {
				this.CurrentPage = 1
				this.orderList = []
				this.getPageData(this.statuss)
			}
			if (this.tactive) {
				this.CurrentPage = 1
				this.orderLists = []
				this.getSellCar(this.statuss)
			}

		},
		//跳转详情
		goDetails(item) {
			uni.navigateTo({
				url: '/pages_Sxy/Sxy_SellOrderDetail/Sxy_SellOrderDetail?contractid=' + item
			})
		},

		dPhone(data) {
			Core.callPhone(data)
		},
		xiaoxi() {
			uni.showToast({
				title: "该功能还未开发",
				icon: "none",
			})
		},
	}
}
